package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "2021000119682892";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCebGUBXbf/rvEqzkI6KMtpqUPPh++SDSocv0eO7F3Tu6EvtBkctlATeNLXavvuxX/B9w+4gXQLxhfngSExAse6yTcngAlVLbSlQWal8pEV//WYiGddxf/T27iciYdIgz0DD0EJTIMnThXYZUZVopvyYZfbt+MEX+fKXGUI60OQU8Brgx08T6O1ftRiXbMkPwyQfaXn+xkB0ylXSFvtoVOEFHGAJar6OBmUTLxbixAjzATz9gchi/tb0Hs1EdtcdsufkLPS+D+FkS/4gfEfvjBlFlCqoJ4UIgGoSEChVMPSzpshkFelL2GN4iRN6VBSD1j60iAyZjUAO4PccXPV8nofAgMBAAECggEAa45P7OOB3lcZaORr1iyIHY5LeYRixBen9ahvnrse4HdMcaSmP/YO24l2K8Z4kJ0JpdhklfFdpfixlfz3Wdz3WPrvNSQaMfg74s5KMHREyFjkltBUzTd1/pH3/l7ot/RR4dfjYbrOeErtTeNEIzOpCJ/1h7iKqhequJqOBcYkMsFyjrrwlvCe2AGbEaFKf+IuUnV0MPAA/sLCyfoJuS6J2Owvs0MLuVjK35lj8POoMht8uzzpVQGljAwlSTsYW4uz0icpIh8OkmAvD1SwNE20z3C/5TdATDN5i2VZRuCxoFIaPOuipxXghMXxmdMAV+kLYdntxxghotLuHY4I1WauMQKBgQD2wFGGR9dS80P2ZH7mlR2rVZ44WliyPyp4+1uOWELbEA7zCp2WlFUAuWgjow661sYUTnKuMjk8aMIZKpIP2OHq93fOM9BDOyEBA2ma5K3+g84cZM5EP7i9IC15ls3q8w5cUI8ldfrE97rPrSjDJAS8FeQn+/W8bdNuJEgzcJa0owKBgQCkXIiZaw5QCqo9Jhva+Ig6zck9BnhU4qZ4edymEoT9mCF50DoOLo7CgMRs14LlrsRVNBm4RbC+qdxgbA4GpUkkGdn62FWPuVEj7MD6IhTq+k0ZOyjRjbB/oKH7nbcSILvHAmfanFYiY16ti5Bhqn65alYN1xpbz1tXS8NC4n2AVQKBgQCiP4Ebr595TrYQjLIYwlI499pKqbWKtxghZv4j0gtb6eo6i29ltPFTwZYlYLFtz419irvDqQiTnTqLu79BJlYe30vqzY0SuA2SkgDdtaJnKZncd7hdjhzWVxMv0SZjaCC4OMx3UGVtIIu4xN55ufRXDHnyhK1nkjulP7wt/slJywKBgETFBs6SUYnB/JrbQBptlKpuAf5Ln7t8ncSY0G6YHxbwOUl7i171sfvF3unffkBV0IYJ550HY71S2wdpDrFPJPw66M6hB6O8GSwb2l6ONgxfMDUXYKvVSf0Mm9noknm+QnMxGX0JnqyCwVGBrKaArMctq9Xco4W4i/JJm+h3VaWFAoGBALlzHLJq2jT0km6c1GTua3N8ggnYCU5fqOHwxZr7cGDmuZC9mKM+SwVVmfjG/cMOupYRLjNk8sOsGpVFWzAcHA36J/l1n4+oE01vDTJggDj43DmuFC7kz5T8htejLcXx/+Ud42PYhH1cEQ2wOTlAUfMF5IjaqRGXrJc6OjL12v8K";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwbuWAsQEdQRRrjRmnIFWCujsnhXcCP7yclUsSyTJTDQctsz96jP9mjKRdKDEnZCOqQXnrAQpz8RLXKrH9k23rFPWePSG6+okbM6MPR2E9eTk1T3G//vUfyzjjNFlXUB0o6NfT83M05EWFBOpPoM+Ht4js9T3leAbGdgLkw1YPh0JFANCFhbnNk2SQHFkJusLYG3MkzA8+4qx08o1IVGxzj1PGzStrTNjrHLJGvEC1Tjqm0HTbCuhQn26pUfvJdkNus2eXieNz0elgodcD0Ner31w0jPEC+B5Khv19m3LTkmbvKURZ0jV7CWiSYaPly2k4YE/UEQQMN92ccOGmyynWwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://sjl.mynatapp.cc.vaiwan.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
